package com.awspaas.user.apps.sts.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class CommonUtil {

    /**
     *  获取日期字符串
     * @param date
     * @param str
     * @return
     */
    public static String getCurCommonDateStr(Date date, String str){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(str);
        String format = simpleDateFormat.format(date);
        return format;
    }

    /**
     *  获取当前时间字符串     yyyy-MM-dd
     * @return
     */
    public static String getCurDateStrLine(){
        return getCurCommonDateStr(new Date(),"yyyy-MM-dd");
    }

    /**
     * 获取当前时间字符串     yyyyMMdd
     * @return
     */
    public static String getCruDateStr(){
        return getCurCommonDateStr(new Date(),"yyyyMMdd");
    }

    public static String dateToDateStr(Date date){
        return getCurCommonDateStr(date,"yyyyMMdd");
    }

    /**
     *  yyyy-mm-dd 转date
     */
    public static Date getDateStrToDate(String dateStr) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(dateStr);
        return date;
    }

    /**
     *
     */
    public static String getCommonDateToStr(Date date,String dateStr){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateStr);
        String format = simpleDateFormat.format(date);
        return format;
    }

    /**
     *  yyyy-mm-dd 转date
     */
    public static Date getCommonStrToDate(String dateStr) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(dateStr);
        return date;
    }

    /**
     *  字符串指定格式 转 date
     */
    public static Date getCommonStrToDate(String dateStr,String format) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = simpleDateFormat.parse(dateStr);
        return date;
    }

    /**
     *  字符串指定格式 转 date
     */
    public static LocalDate getCommonStrToLocalDate(String dateStr,String format) throws ParseException {
        LocalDate localDate = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(format));
        return localDate;
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCurDateTimeStr(){
        return getCurCommonDateStr(new Date(),"yyyy-MM-dd HH:mm:ss");
    }

    /**
     * Date转LocalDate
     * @param date
     * @return
     */
    public static LocalDate dateToLocalDate(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        return localDate;
    }

    /**
     * Date转LocalDate
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    public static LocalDate getCurLocalDate(){
        return LocalDate.now();
    }

    public static String getLocalDateToStr(LocalDate localDate){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 定义日期格式
        String formattedDate = localDate.format(formatter); // 将LocalDate对象转换为字符串
        return formattedDate;
    }

    /**
     *  date
     * @param date
     * @return
     */
    public static LocalDate dateStrToLocalDate(String date){
        return LocalDate.parse(date,DateTimeFormatter.ISO_DATE);
    }



}
